﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
  public class ImgArticleService
    {
      /// <summary>
      /// 获取所有消息回复
      /// </summary>
      /// <returns></returns>
      public DataTable GetList()
      {
          string sql = "select Id,Title from TB_ImgArticle where del=0";
          return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
      }

      /// <summary>
      /// 编辑文章
      /// </summary>
      /// <param name="entity"></param>
      /// <returns></returns>
      public int Edit(TB_ImgArticle entity)
      {
          SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Id",SqlDbType.Int),
                new SqlParameter("@Title",SqlDbType.VarChar,150),
                new SqlParameter("@Content",SqlDbType.Text), 
                new SqlParameter("@Date",SqlDbType.DateTime),
                new SqlParameter("@Del",SqlDbType.Int),
                new SqlParameter("@Img",SqlDbType.VarChar,50),
                new SqlParameter("@Des",SqlDbType.VarChar,500),
                new SqlParameter("@Url",SqlDbType.VarChar,200),
            };
          parameters[0].Value = entity.Id;
          parameters[1].Value = entity.Title;
          parameters[2].Value = entity.Content;
          parameters[3].Value = entity.Date;
          parameters[4].Value = entity.Del;
          parameters[5].Value = entity.Img;
          parameters[6].Value = entity.Des;
          parameters[7].Value = entity.Url;
          string sql = entity.Id > 0
              ? "update TB_ImgArticle set Content=@Content,Img=@Img,des=@Des,date=@Date,url=@Url,title=@Title where id=@Id"
              : "insert into [dbo].[TB_ImgArticle](Content,img,[des],[date],url,title,del)values(@Content,@Img,@Des,@Date,@Url,@Title,0)"
              ;
          return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
          //if (entity.Id > 0)
          //{
          //    string sql = "update TB_ImgArticle set Content=@Content,Img=@Img,des=@Des,date=@Date,url=@Url,title=@Title where id=@Id";
          //    return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
          //}
          //if (entity.Id == 0)
          //{
          //    string sql = "insert into [dbo].[TB_ImgArticle](Content,img,[des],[date],url,title,del)values(@Content,@Img,@Des,@Date,@Url,@Title,0) SELECT @@IDENTITY";
          //    var id = SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql, parameters);
          //}
      }

      public DataSet GetList(int pageIndex, int pageCount, string title)
      {
          SqlParameter parameter = new SqlParameter("@title", SqlDbType.VarChar, 100);
          parameter.Value = title;
          string where = "";
          where = title == "" ? where : (where + "and title like  N'%'+@title+'%'");
          string sql = "select count(*) as count from [dbo].[TB_ImgArticle] "+(where!=""?("where "+where.Substring(4,where.Length-4)):" ")+"  " +
                       "select top(" + pageCount + ")Id,Img,Des,Date,Url,Title from [dbo].[TB_ImgArticle] where id not in(select top(" + pageIndex + ")Id from [dbo].[TB_ImgArticle] order by id desc) and del=0 " + where + "  order by id desc ";
          var set= SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql, parameter);
          set.Tables[0].TableName = "count";
          set.Tables[1].TableName = "data";
          return set;
      }
    }
   
}
